import 'package:dio/dio.dart';

import 'network_config.dart';

// 请求方式
enum HttpMethod { get, post, put, delete }

/// 请求工具类
class NetworkManager {
  static final BaseOptions baseOptions = BaseOptions(
      baseUrl: NetworkConfig.baseURL,
      connectTimeout: NetworkConfig.connectTimeOut);
  static final Dio dio = Dio(baseOptions);

  static Future<T> request<T>(String url,
      {HttpMethod method = HttpMethod.get,
      Map<String, dynamic>? params,
      Interceptor? inter}) async {
    // 1.创建单独配置
    final options = Options(method: _getMethod(method));
    // 2.发送网络请求
    try {
      Response response =
          await dio.request(url, queryParameters: params, options: options);
      return response.data;
    } on DioError catch (e) {
      return Future.error(e);
    }
  }

  static String _getMethod(HttpMethod httpMethod) {
    switch (httpMethod) {
      case HttpMethod.get:
        return 'get';
      case HttpMethod.post:
        return 'post';
      case HttpMethod.put:
        return 'put';
      case HttpMethod.delete:
        return 'delete';
      default:
        return "--";
    }
  }
}
